/*******************************************************************************
* Copyright (c) 2012 
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* zhangliang - initial API and implementation
*******************************************************************************/

#ifndef SINGLETON_H
#define SINGLETON_H

template<class T>
class Singleton : boost::noncopyable {
 public:
  static T& Instance(void) {
    if (!inst_) {
      boost::mutex::scoped_lock l(Singleton::init_mutex_);
      if (!inst_) {
        inst_ = new T;
      }
    }
    return *inst_;
  }
 private:
  static T* inst_;
  static boost::mutex init_mutex_;
};

template<class T>
T* Singleton<T>::inst_ = NULL;

template<class T>
boost::mutex Singleton<T>::init_mutex_;

#endif // SINGLETON_H

